package com.my.learn.datestructure;

/**
 * @Author Godzilla
 * @Date 2018/12/14
 * @Version V1.0.0
 * @Update 更新说明
 */
public class DoubleLink<T> {

    private DNode<T> mHead;

    private int mCount;

    private class DNode<T> {
        public DNode<T> next;
        public DNode<T> prev;
        public T value;

        public DNode(DNode<T> next, DNode<T> prev, T value) {
            this.next = next;
            this.prev = prev;
            this.value = value;
        }
    }

    public DoubleLink() {
        mHead = new DNode(null, null, null);
        mHead.next = mHead.prev = mHead;
        mCount = 0;
    }

    public int size() {
        return mCount;
    }

    // 返回链表是否为空
    public boolean isEmpty() {
        return mCount == 0;
    }

    private DNode<T> getNode(int index) {
        if (index < 0 || index >= mCount) {
            throw new IndexOutOfBoundsException();
        }
        if (index < mCount / 2) {
            DNode<T> node = mHead.next;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            return node;
        }
        DNode<T> rnode = mHead.prev;
        int rindex = mCount - index - 1;
        for (int i = 0; i < rindex; i++) {
            rnode = rnode.prev;
        }
        return rnode;

    }

    public T get(int index) {
        return getNode(index).value;
    }

    public T getFirst() {
        return getNode(0).value;
    }

    public T getLast() {
        return getNode(mCount).value;
    }

    public void insert(int index, T t) {

    }

    public void insertFirst(T t) {

    }

    public void appendLast(T t) {

    }

    public void del(int index) {

    }

    public void deleteFirst() {

    }

    public void deleteLast() {

    }
}
